import random
a= int(input("Введите четырехзначное число"))
max=0
if a>9999 or a<1000:
    print(0)
def prost(b):
    if b <= 1:
        return False
    for _ in range(5):
        m=random.randint(1,b-1)
    if pow(m,b-1,b)!=1:
        return False
    return True
    
c=a//1000
if prost(c) == True and c > max:
    max = c#1
    
d=a//100
if prost(d) == True and d > max:
    max = d#2
    
e=a//10
if prost(e) == True and e > max:
    max = e#3
    
f=a
if prost(f) == True and f > max:
    max = f#4
    
g=a//100%10
if prost(g) == True and g > max:
    max = g#5
    
h=a//10%100
if prost(h) == True and h > max:
    max = h#6
    
i=a%1000
if prost(i) == True and i > max:
    max = i#7
    
j=a//10%10
if prost(j) == True and j > max:
    max = j#8
    
k=a%100
if prost(k) == True and k > max:
    max = k#9
    
l=a%10
if prost(l) == True and l > max:
    max = l#10

print(max)